package top.xinsix.est.service.impl;
import com.alibaba.fastjson.JSON;
import top.xinsix.est.dto.*;
import top.xinsix.est.mapper.BrandMapper;
import top.xinsix.est.mapper.ClassifyMapper;
import top.xinsix.est.mapper.ModelMapper;
import top.xinsix.est.pojo.Classify;
import top.xinsix.est.req.ModelTypeReq;
import top.xinsix.est.service.ClassifyService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import top.xinsix.est.constants.Constants;
import top.xinsix.est.tools.EmptyUtils;
import top.xinsix.est.tools.redis.RedisKeyUtils;
import top.xinsix.est.tools.redis.RedisUtils;

import javax.annotation.Resource;

@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Resource
    private ClassifyMapper classifyMapper;

    @Resource
    private BrandMapper brandMapper;
    @Resource
    private ModelMapper modelMapper;

    @Resource
    private RedisUtils redisUtils;

    public Classify getClassifyById(Long id){
        return classifyMapper.getClassifyById(id);
    }

    public List<Classify>	getClassifyListByMap(Map<String,Object> param){
        return classifyMapper.getClassifyListByMap(param);
    }

    public Integer getClassifyCountByMap(Map<String,Object> param){
        return classifyMapper.getClassifyCountByMap(param);
    }

    public Integer qdtxAddClassify(Classify classify){
            classify.setCreatdTime(new Date());
            return classifyMapper.insertClassify(classify);
    }

    public Integer qdtxModifyClassify(Classify classify){
        classify.setUpdatedTime(new Date());
        return classifyMapper.updateClassify(classify);
    }

    public Integer qdtxDeleteClassifyById(Long id){
        return classifyMapper.deleteClassifyById(id);
    }

    public Integer qdtxBatchDeleteClassify(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return classifyMapper.batchDeleteClassify(param);
    }

    public Page<Classify> queryClassifyPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = classifyMapper.getClassifyCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Classify> classifyList = classifyMapper.getClassifyListByMap(param);
        page.setList(classifyList);
        return page;
     }

    public List<ClassifyDto> getTree() {
        List<ClassifyDto> classifyDtoList=null;
        //判断redis中是否已经存储了数据
        //est_home_classifyTree:
        String key= RedisKeyUtils.formatKeys(Constants.Redis.PREFIX,Constants.Redis.KEY_HOME_TREE);
        classifyDtoList=redisUtils.getJsonList(key,ClassifyDto.class);
        if(classifyDtoList!=null){
            return classifyDtoList;
        }
        //存在，直接获取返回
        //不存在，就需要冲数据库获取
        //查询分类列表
        classifyDtoList=classifyMapper.findClassifyDtoList(Constants.Model.Status.SALE);
        //根据分类Id查询品牌列表
        for (ClassifyDto classifyDto:classifyDtoList) {
            if(classifyDto!=null&&classifyDto.getId()!=null){
                List<BrandDto> brandDtoList=brandMapper.findBrandDtoListByCId(classifyDto.getId(),Constants.Model.Status.SALE);
                //根据品牌id查询商品型号（类型）列表
                for(BrandDto  brandDto:brandDtoList){
                    if(brandDto!=null&&brandDto.getId()!=null){
                        //Long brandId, Long classifyId, Integer status
                        ModelTypeReq modelTypeReq=new ModelTypeReq(brandDto.getId(),classifyDto.getId(),Constants.Model.Status.SALE);
                        List<ModelTypeDto> modelTypeDtoList=modelMapper.findModelTypeByBId(modelTypeReq);
                        brandDto.setModelList(modelTypeDtoList);
                    }
                }
                classifyDto.setBrandList(brandDtoList);
            }
        }
        //将数据添加到redis
        redisUtils.set(key, JSON.toJSONString(classifyDtoList));
        return classifyDtoList;
    }


}
